﻿/**
*	The MIT License
*
*	<Samsung GT-S5603 Wallpaper Creator>
*	Copyright (C) <2009>  <Anindya Chatterjee>
*
*	Permission is hereby granted, free of charge, to any person obtaining a copy
*	of this software and associated documentation files (the "Software"), to deal
*	in the Software without restriction, including without limitation the rights
*	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*	copies of the Software, and to permit persons to whom the Software is
*	furnished to do so, subject to the following conditions:
*
*	The above copyright notice and this permission notice shall be included in
*	all copies or substantial portions of the Software.
*
*	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*	THE SOFTWARE.
*
*/

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;

namespace SamsungWallpaper
{
    public partial class MainForm : Form
    {
        private static bool _notSaved;

        public MainForm()
        {
            InitializeComponent();
        }

        private void pictureOne_DragEnter(object sender, DragEventArgs e)
        {
            PanelDragEnter(e);
        }

        private void pictureOne_DragDrop(object sender, DragEventArgs e)
        {
            PanelDragDrop(e, pictureOne);
        }

        private void pictureTwo_DragEnter(object sender, DragEventArgs e)
        {
            PanelDragEnter(e);
        }

        private void pictureTwo_DragDrop(object sender, DragEventArgs e)
        {
            PanelDragDrop(e, pictureTwo);
        }

        private void pictureThree_DragDrop(object sender, DragEventArgs e)
        {
            PanelDragDrop(e, pictureThree);
        }

        private void pictureThree_DragEnter(object sender, DragEventArgs e)
        {
            PanelDragEnter(e);
        }

        private static void PanelDragEnter(DragEventArgs e)
        {
            e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop) ? DragDropEffects.Copy : DragDropEffects.None;
        }

        private static void PanelDragDrop(DragEventArgs e, Control panel)
        {
            var filePaths = (string[]) e.Data.GetData(DataFormats.FileDrop, false);
            string file = (filePaths.Length > 0) ? filePaths[0] : "";
            Image img = Image.FromFile(file);
            if (img.Height == 320 && img.Width == 240)
            {
                panel.BackgroundImage = img;
                _notSaved = true;
            }
            else
                MessageBox.Show(
                    "This image's resolution is not suitable for Wallpaper.\n\tSuitable resolution is 320x240.",
                    img.Width + "x" + img.Height + " Image Not Supported");
        }

        private void buttonPreview_Click(object sender, EventArgs e)
        {
            var bmpOne = (Bitmap) pictureOne.BackgroundImage;
            var bmpTwo = (Bitmap) pictureTwo.BackgroundImage;
            if (bmpOne != null && bmpTwo != null)
            {
                int width = bmpOne.Width + bmpTwo.Width;
                int height = bmpOne.Height;
                var combineBmp = new Bitmap(width, height);
                Graphics gr = Graphics.FromImage(combineBmp);
                gr.DrawImage(bmpOne, 0, 0, bmpOne.Width, bmpOne.Height);
                gr.DrawImage(bmpTwo, bmpOne.Width, 0, bmpTwo.Width, bmpTwo.Height);

                var bmpThree = (Bitmap) pictureThree.BackgroundImage;
                int finalWidth = combineBmp.Width + bmpThree.Width;
                var finalBmp = new Bitmap(finalWidth, height);
                Graphics fgr = Graphics.FromImage(finalBmp);
                fgr.DrawImage(combineBmp, 0, 0, combineBmp.Width, combineBmp.Height);
                fgr.DrawImage(bmpThree, combineBmp.Width, 0, bmpThree.Width, bmpThree.Height);

                picturePreview.BackgroundImage = finalBmp;
                tabHost.SelectedTab = tabPagePreview;
                tabPagePreview.Focus();
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            var sdlg = new SaveFileDialog {Filter = "Png Image |*.png|Jpeg Image|*.jpg"};
            if (sdlg.ShowDialog() == DialogResult.OK && picturePreview.BackgroundImage != null)
            {
                picturePreview.BackgroundImage.Save(sdlg.FileName);
                _notSaved = false;
            }
        }

        private void linkConfig_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            new About
                {
                    StartPosition = FormStartPosition.CenterScreen
                }.ShowDialog(this);   
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_notSaved)
            {
                DialogResult result = MessageBox.Show("Wallpaper created is not saved!\nAre you sure to exit?", "Confirm",
                                MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                e.Cancel = (result != DialogResult.Yes);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            UpdateUtil.CheckForUpdate();
        }

        private void buttonDonate_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Would you consider to Donate something via Paypal?", "Donation", MessageBoxButtons.YesNo);
            if(result == DialogResult.Yes)
            {
                Process.Start("https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=1787302");
            }
        }
    }
}